<?php
class mysql{
    function connect($dbhost, $dbuser, $dbpw, $dbname = '',$dbcharset='') {
        if(!@mysql_connect($dbhost, $dbuser, $dbpw)) {
            $this->show('Can not connect to MySQL server');
        }
        if($dbname) {
            $this->select_db($dbname);
        }
        if($this->version() > '4.1') {
            if($dbcharset) {
                $this->query("SET NAMES '".$dbcharset."'");
            }
        }
    }

    function select_db($dbname) {
        return mysql_select_db($dbname);
    }

    function fetch_array($query, $result_type = MYSQL_ASSOC) {
        return @mysql_fetch_array($query, $result_type);
    }

    function query($sql, $type = '') {
        if(!($query = mysql_query($sql))) $this->show('MySQL Query Error', $sql);
        return $query;
    }

    function affected_rows() {
        return mysql_affected_rows();
    }

    function result($query, $row) {
        return mysql_result($query, $row);
    }

    function num_rows($query) {
        return @mysql_num_rows($query);
    }

    function num_fields($query) {
        return mysql_num_fields($query);
    }

    function free_result($query) {
        return mysql_free_result($query);
    }

    function insert_id() {
        return mysql_insert_id();
    }

    function fetch_row($query) {
        return mysql_fetch_row($query);
    }

    function version() {
        return mysql_get_server_info();
    }

    function close() {
        return mysql_close();
    }

    function show($message = '', $sql = '') {
        if(!$sql) echo $message;
        else echo $message.'<br>'.$sql;
    }
}
$db = new mysql();
$db->connect('hdm189315587.my3w.com', 'hdm189315587', 'dianduidian2018', 'hdm189315587_db','utf8');
$sql = "select * from pb_wechat_user where subscribe = 1 and review_status = 1";
$query = $db->query($sql);
$temp = array();
$temps = array();
if($db->num_rows($query)){
    $i = 0;
    while($row = $db->fetch_array($query)){
        $sum = (time() - $row['subscribe_time'])/3600/24;  // 总天数
        $sqls = "select * from pb_sign where openid = '".$row['openid']."'";
        $querys = $db->query($sqls);
        $time = $db->num_rows($querys);
        $percent = round(($time/$sum),3);   // 签到比
        $sqlss = "select * from pb_summary where type = 1 and openid = '".$row['openid']."'";
        $queryss = $db->query($sqlss);
        $times = $db->num_rows($queryss);
        $percents = round(($times/$sum),3);   // 总结比
        $temp[$i]['percent'] = $percent;
        $temp[$i]['openid'] = $row['openid'];
        $temps[$i]['percents'] = $percents;
        $temps[$i]['openid'] = $row['openid'];
        $i++;
    }
}
$arr = array_map(create_function('$n', 'return $n["percent"];'), $temp);
array_multisort($arr,SORT_DESC,$temp);//多维数组的排序
foreach ($temp as $key => $value){
    $keys = $key+1;
    $sql = "update  pb_wechat_user set percent = ".$value['percent'].",rank = ".$keys." where openid = '".$value
        ['openid']."'";
    $db->query($sql);
}
$arrs = array_map(create_function('$m', 'return $m["percents"];'), $temps);
array_multisort($arrs,SORT_DESC,$temps);//多维数组的排序
foreach ($temps as $key => $value){
    $keys = $key+1;
    $sqls = "update  pb_wechat_user set percents = ".$value['percents'].",ranks = ".$keys." where openid = '".$value
        ['openid']."'";
    $db->query($sqls);
}
?>